import 'package:flutter/material.dart';
import 'package:simple/pub/Storage.dart';

class TestRefresh extends StatefulWidget {
  @override
  _TestRefreshState createState() => _TestRefreshState();
}

class _TestRefreshState extends State<TestRefresh> {
  int _itemCount;
  int _page = 1;
  List _list = [];

  getdata() async {
    var res = await Sev().get('agent/purchase/order/list/$_page/10');
    int code = res['code'];
    if (code == 200) {
      print('okokokokoko');
      setState(() {
        _list.addAll(res['data']['list']);
      });
    } else {
      print('errrrror');
    }
  }

  Widget _itemBuilder(BuildContext context, int index) {
    return new Padding(
        key: new Key(index.toString()),
        padding: new EdgeInsets.all(10.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            new Text(
              "${_list[index]['id']}",
              style: new TextStyle(fontSize: 14.0),
            ),
            Text('${_list[index]['createTime']}'),
          ],
        ));
  }

  @override
  void initState() {
    _itemCount = 10;
    _page = 1;
    getdata();
    super.initState();
  }

  Future<Null> onFooterRefresh() {
    getdata();
    return new Future.delayed(new Duration(seconds: 2), () {
      setState(() {
        _itemCount += 10;
        _page++;
      });
    });
  }

  Future<Null> onHeaderRefresh() {
    // throw new Exception("");

    return new Future.delayed(new Duration(seconds: 2), () {
      setState(() {
        _itemCount = 10;
        _page = 1;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('data'),
        ),
        body: new SafeArea(
            child: ListView.builder(
            itemBuilder: _itemBuilder,
            itemCount: _list.length,
          ),
        ));
  }
}
